package spring.resource;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.Executors;


import org.apache.derby.impl.io.vfmem.VirtualFile;
import org.apache.ibatis.io.VFS;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.VfsResource;
import org.testng.Assert;
import org.testng.annotations.Test;


public class ResourceTest {

	@Test
	public void testByteArrayResource() {
		Resource resource = new ByteArrayResource("Hello World".getBytes());
		if (resource.exists()) {
			dumpStream(resource);
		}
	}

	private void dumpStream(Resource resource) {
		InputStream is = null;
		try {
			is = resource.getInputStream();

			byte[] descBytes = new byte[is.available()];
			is.read(descBytes);
			System.out.println(new String(descBytes));
		} catch (Exception e) {
			e.printStackTrace();
		} finally {

			try {
				is.close();
			} catch (IOException e) {

				e.printStackTrace();
			}
		}

	}

	@Test
	public void testInputStreamResource() {
		ByteArrayInputStream bis = new ByteArrayInputStream("Hello wordld!".getBytes());
		Resource resource = new InputStreamResource(bis);
		if (resource.exists()) {
			dumpStream(resource);
		}
		Assert.assertEquals(true, resource.isOpen());

	}

	@Test
	public void testFileResource() {
		File file = new File("d://test.txt");
		Resource resource = new FileSystemResource(file);
		if (resource.exists()) {
			dumpStream(resource);
		}
		Assert.assertEquals(false, resource.isOpen());

	}

	@Test
	public void testClasspathResourceByDefaultClassLoader() throws IOException {
		Resource resource = new ClassPathResource("src/main/resources/test1.properties");
		if (resource.exists()) {
			dumpStream(resource);
		}
		System.out.println("path" + resource.getFile().getAbsolutePath());
		Assert.assertEquals(false, resource.isOpen());
	}

	@Test
	public void testClasspathResourceByClassLoader() throws IOException {
		ClassLoader cl = this.getClass().getClassLoader();
		Resource resource = new ClassPathResource("com/spring/resource/test1.properties", cl);
		if (resource.exists()) {
			dumpStream(resource);
		}
		System.out.println("path:" + resource.getFile().getAbsolutePath());
	}

	@Test
	public void testClasspathResourceByClass() throws IOException {
		Class clazz = this.getClass();
		Resource resource1 = new ClassPathResource(
                "src/main/resources/file/test1.properties", clazz);
		if (resource1.exists()) {
			dumpStream(resource1);
		}
		System.out.println("path:" + resource1.getFile().getAbsolutePath());
		Assert.assertEquals(false, resource1.isOpen());

		Resource resource2 = new ClassPathResource("src/main/resources/test1.properties", this.getClass());
		if (resource2.exists()) {
			dumpStream(resource2);
		}
		System.out.println("path:" + resource1.getFile().getAbsolutePath());
	}
	
	@Test
	public void classPathResourceTestFormJar() throws IOException{
		
		Resource resource=new ClassPathResource("/overvier.html");
		if(resource.exists()){
			dumpStream(resource);
		}
		System.out.println("path:"+resource.getURL().getPath());
		Assert.assertEquals(false, resource.isOpen());
	}

	
}
